package com.university.fragment;

import java.lang.reflect.Field;
import java.util.ArrayList;

import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SearchView;
import android.widget.Toast;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;

import com.example.myuniversity.MajorDetailsActivity;
import com.example.myuniversity.R;
import com.university.utils.SqliteDatabaseManager;

public class MajorFragment extends Fragment {
	private View view;
	private ListView listView;
	private SqliteDatabaseManager manager;
	private SQLiteDatabase mDatabase;
	private Cursor cursor;
	private BaseAdapter listadapter;
	private LayoutInflater mInflater;
	private RadioButton radioAll,radioWen,radioLi;
	private SearchView searchView;
	private ArrayList<String> arrayList;
	private String subject;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view=inflater.inflate(R.layout.fragment_major, container, false);
		mInflater=inflater;
		manager=SqliteDatabaseManager.getInstance(container.getContext().getApplicationContext());
		mDatabase=manager.getDatabase();
		subject="全部";
		init();
		setOnClick();
		//反射来获得searchView的自动完成框变量
        Class<?> argClass = searchView.getClass();     
	    //--指定某个私有属性,mSearchPlate是搜索框父布局的名字        
	    Field ownField;
		try {
			ownField = argClass.getDeclaredField("mQueryTextView");
			ownField.setAccessible(true);     
			ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,arrayList);
			//--暴力反射,只有暴力反射才能拿到私有属性        
			AutoCompleteTextView mView = (AutoCompleteTextView) ownField.get(searchView);
			mView.setAdapter(adapter);
			
			mView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					cursor.close();
					if (subject.equals("全部")) {
						 cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最高分","最低分"}, "院校名称=?", new String[]{parent.getItemAtPosition(position).toString()}, "院校代码,最高分", null, null);
					}else {
						cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最高分","最低分"}, "院校名称=? and 文理=?", new String[]{parent.getItemAtPosition(position).toString(),subject}, "院校代码,最高分", null, null);
					}
					listadapter.notifyDataSetChanged();
				}
			});
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}    
	    
	    
		listadapter=new MyAdapter();
		listView.setAdapter(listadapter);
		
		return view;
	}
	
	//页面创建的时候的初始化
	private void init() {
		listView=(ListView) view.findViewById(R.id.listview_major);
		arrayList=new ArrayList<String>();
		radioAll=(RadioButton) view.findViewById(R.id.radio_major_all);
		radioWen=(RadioButton) view.findViewById(R.id.radio_major_wen);
		radioLi=(RadioButton) view.findViewById(R.id.radio_major_li);
		searchView=(SearchView) view.findViewById(R.id.searchview_major);
		searchView.setFocusable(false);
		if (subject.equals("全部")) {
			cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最高分","最低分"}, null, null, "院校代码,最高分", null, null);
		}else {
			cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最高分","最低分"}, "文理=?",new String[]{subject}, "院校代码,最高分", null, null);
		}
		while (cursor.moveToNext()) {
			arrayList.add(cursor.getString(1));
		}
	
	}
	
	private void setOnClick() {
		radioAll.setOnClickListener(new OnRadioClick());
		radioLi.setOnClickListener(new OnRadioClick());
		radioWen.setOnClickListener(new OnRadioClick());
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//因为此时的cursor是搜索过后的新cursur跟listview保持一致
				cursor.moveToPosition(position);
				String uName=cursor.getString(1);
				String uId=cursor.getString(0);
				Intent intent=new Intent(getActivity(), MajorDetailsActivity.class);
				intent.putExtra("SchoolName", uName);
				intent.putExtra("SchoolId", uId);
				startActivity(intent);
			}
		});
		//为searchview设置的内容变化监听
		searchView.setOnQueryTextListener(new OnQueryTextListener() {
			@Override
			public boolean onQueryTextSubmit(String query) {
				cursor.close();
				if (subject.equals("全部")) {
					cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最高分","最低分"}, "院校名称 like ?", new String[]{"%"+query+"%"}, "院校代码,最高分", null, null);
				}else {
					cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最高分","最低分"}, "院校名称 like ? and 文理=?", new String[]{"%"+query+"%",subject}, "院校代码,最高分", null, null);
				}
				listadapter.notifyDataSetChanged();
				return false;
			}
			@Override
			public boolean onQueryTextChange(String newText) {
				if (newText.length()==0) {
					cursor.close();
					if (subject.equals("全部")) {
						cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最高分","最低分"}, null, null, "院校代码,最高分", null, null);
					}else {
						cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最高分","最低分"}, "文理=?",new String[]{subject}, "院校代码,最高分", null, null);
					}
					listadapter.notifyDataSetChanged();
				}
				return false;
			}
		});
	}

	//为3个radiobutton创建的监听类
	class OnRadioClick implements OnClickListener{
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.radio_major_all:
				subject="全部";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最高分","最低分"}, null, null, "院校代码,最高分", null, null);
				break;
			case R.id.radio_major_wen:
				subject="文科";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最高分","最低分"}, "文理=?", new String[]{"文科"}, "院校代码,最高分", null, null);
				break;
			case R.id.radio_major_li:
				subject="理科";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最高分","最低分"}, "文理=?", new String[]{"理科"}, "院校代码,最高分", null, null);
				break;
			default:
				break;
			}
			listadapter.notifyDataSetChanged();
		}
	}
	
	private class MyAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			return cursor.getCount();
		}
		@Override
		public Object getItem(int position) {
			return cursor.moveToPosition(position);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			cursor.moveToPosition(position);
			ViewHolder viewHolder;
			
			if (convertView==null) {
				viewHolder=new ViewHolder();
				convertView=mInflater.inflate(R.layout.layout_major_listview_item, parent, false);
				viewHolder.uId=(TextView) convertView.findViewById(R.id.txt_major_fragment_uid);
				viewHolder.uName=(TextView) convertView.findViewById(R.id.txt_major_fragment_uname);
				viewHolder.majorMax=(TextView) convertView.findViewById(R.id.txt_major_fragment_majormax);
				viewHolder.majorMin=(TextView) convertView.findViewById(R.id.txt_major_fragment_majormin);
				convertView.setTag(viewHolder);
			}else {
				viewHolder=(ViewHolder) convertView.getTag();
			}
			viewHolder.uId.setText(cursor.getString(0));
			viewHolder.uName.setText(cursor.getString(1));
			viewHolder.majorMax.setText(cursor.getString(2));
			viewHolder.majorMin.setText(cursor.getString(3));
			return convertView;
		}
		
	}
	static class ViewHolder{
		TextView uId,uName,majorMax,majorMin;
	}

	@Override
	public void onDestroy() {
		cursor.close();
		super.onDestroy();
	}
	
}
